home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / dos / io / setmode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  1.3 KB  |  56 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <sys/exceptn.h>
  4. #include <dpmi.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <io.h>
  8.  
  9. #include <libc/dosio.h>
  10.  
  11. void (*__setmode_stdio_hook)(int fd, int mode); /* BSS to zero */
  12.  
  13. int
  14. setmode(int handle, int mode)
  15. {
  16.   __dpmi_regs regs;
  17.   int oldmode, newmode;
  18.  
  19.   regs.x.ax = 0x4400;
  20.   regs.x.bx = handle;
  21.   regs.x.dx = 0;        /* Clear upper e-word for return */
  22.   __dpmi_int(0x21, ®s);
  23.   if (regs.x.flags & 1)
  24.   {
  25.     errno = __doserr_to_errno(regs.x.ax);
  26.     return -1;
  27.   }
  28.   oldmode = newmode = regs.x.dx;
  29.  
  30.   if (mode & O_BINARY)
  31.     newmode |= 0x20;
  32.   else
  33.     newmode &= ~0x20;
  34.  
  35.   if (oldmode & 0x80)    /* Only for character dev */
  36.   {
  37.     regs.x.ax = 0x4401;
  38.     regs.x.bx = handle;
  39.     regs.x.dx = newmode & 0xff;           /* Force upper byte zero */
  40.     __dpmi_int(0x21, ®s);
  41.     if (regs.x.flags & 1)
  42.     {
  43.       errno = __doserr_to_errno(regs.x.ax);
  44.       return -1;
  45.     }
  46.     if (handle == 0)
  47.       __djgpp_set_ctrl_c(!(mode & O_BINARY));
  48.   }
  49.  
  50.   oldmode = __file_handle_modes[handle] & (O_BINARY|O_TEXT);
  51.   __file_handle_modes[handle] &= ~(O_BINARY|O_TEXT);
  52.   __file_handle_modes[handle] |= (mode & (O_BINARY|O_TEXT));
  53.  
  54.   return oldmode;
  55. }
  56.